package com.ankiang.treeprint.fragment.components;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.ankiang.treeprint.R;
import com.ankiang.treeprint.base.BaseFragment;
import com.ankiang.treeprint.lib.TRDataManager;
import com.ankiang.treeprint.lib.annotation.Widget;
import com.ankiang.treeprint.manager.QDDataManager;
import com.ankiang.treeprint.view.TRConfig;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * {@link QMUIGroupListView} 的使用示例。
 * Created by Kayo on 2016/11/21.
 */

@Widget(widgetClass = TRSettingsFragment.class, iconRes = R.drawable.ic_icon_settings)
public class TRSettingsFragment extends BaseFragment {

    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;

    @BindView(R.id.groupListView)
    QMUIGroupListView mGroupListView;



    private QDDataManager.QDItemDescription mQDItemDescription;

//    // 保存数据
//    private int itemId;
//    private TRItemTree itemTree;


    // 配置
    private TRConfig conf;
    private TRDataManager dataManager;

    private QMUICommonListItemView showVertical;    // 树的方向
    private QMUICommonListItemView showImg;         // 头像
    private QMUICommonListItemView showRowTips;     // 行间隔
    private QMUICommonListItemView showColumnTips;  // 列间隔
    private QMUICommonListItemView useCustomedImg;  // 是否使用用户自定义的图片

    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.tr_fragment_settings, null);
        ButterKnife.bind(this, root);

        conf = TRConfig.getInstance();
        dataManager = TRDataManager.getInstance(getContext());

        mQDItemDescription = QDDataManager.getInstance(getContext()).getDescription(this.getClass());


        initTopBar();

        initGroupListView();

        return root;
    }

    private void initTopBar() {
        mTopBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popBackStack();
                if(callback!=null){
                    callback.afterOperation(0);
                }
            }
        });

        mTopBar.setTitle(mQDItemDescription.getName());
    }



    private void initGroupListView() {


        showVertical = mGroupListView.createItemView("垂直展示");
        showVertical.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        showVertical.getSwitch().setChecked(conf.isShowVertical());
        showVertical.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(getActivity(), V(R.string.settings_done_tips), Toast.LENGTH_SHORT).show();
                conf.flipShowVertical();
            }
        });


        showImg = mGroupListView.createItemView("显示头像");
        showImg.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        showImg.getSwitch().setChecked(conf.isShowImg());
        showImg.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(getActivity(), V(R.string.settings_done_tips), Toast.LENGTH_SHORT).show();
                conf.flipShowImg();
            }
        });

        showColumnTips = mGroupListView.createItemView("显示列间隔");
        showColumnTips.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        showColumnTips.getSwitch().setChecked(conf.isShowColumnTips());
        showColumnTips.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(getActivity(), V(R.string.settings_done_tips), Toast.LENGTH_SHORT).show();
                conf.flipShowColumnTips();
            }
        });

        showRowTips = mGroupListView.createItemView("显示行间隔");
        showRowTips.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        showRowTips.getSwitch().setChecked(conf.isShowRowTips());
        showRowTips.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(getActivity(), V(R.string.settings_done_tips), Toast.LENGTH_SHORT).show();
                conf.flipShowRowTips();
            }
        });


        useCustomedImg = mGroupListView.createItemView("使用自定义头像");
        useCustomedImg.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        useCustomedImg.getSwitch().setChecked(conf.isUseCustomedImg());
        useCustomedImg.getSwitch().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(getActivity(), V(R.string.settings_done_tips), Toast.LENGTH_SHORT).show();
                conf.flipUseCustomedImg();
            }
        });


        QMUIGroupListView.newSection(getContext())
                .setTitle(V(R.string.settings_tree))
                .addItemView(showVertical, null)
                .addItemView(showImg, null)
                .addItemView(showRowTips, null)
                .addItemView(showColumnTips, null)
                .addItemView(useCustomedImg, null)
                .addTo(mGroupListView);


    }


}
